package com.jss.lanouhn.fristapp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.Interface.OnItemClickLister;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.CategoryLastActivity;
import com.jss.lanouhn.fristapp.activity.CategoryRealLastActivity;
import com.jss.lanouhn.fristapp.activity.CategoryStrategyActivity;
import com.jss.lanouhn.fristapp.activity.HomeTitleActivity;
import com.jss.lanouhn.fristapp.adapters.CategoryStrategyNormalAdapter;
import com.jss.lanouhn.fristapp.adapters.CategoryStrategyTopAdapter;
import com.jss.lanouhn.fristapp.modles.CategoryStrategyNormal;
import com.jss.lanouhn.fristapp.modles.CategoryStrategyTop;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/25.
 */
public class Category_Strategy_Fragment extends Fragment {
    private RecyclerView recyclerView1, recyclerView2, recyclerView3, recyclerView4;
    private String url = "http://api.liwushuo.com/v2/columns?limit=11&offset=0";
    private String url1 = "http://api.liwushuo.com/v2/channel_groups/all";
    private CategoryStrategyTop categoryStrategyTop;
    private CategoryStrategyNormal categoryStrategtNormal;
    private CategoryStrategyNormalAdapter csnAdapter2;
    private CategoryStrategyNormalAdapter csnAdapter3;
    private CategoryStrategyNormalAdapter csnAdapter4;
    private CategoryStrategyTopAdapter cstAdapter;
    private TextView tv1, tv2, tv3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_category_strategy, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initDateForwebTop();
        initDateForwebAll();
    }

    private void initView() {
        recyclerView1 = (RecyclerView) getView().findViewById(R.id.rv_category_strategy);
        recyclerView2 = (RecyclerView) getView().findViewById(R.id.rv1_category_strategy);
        recyclerView3 = (RecyclerView) getView().findViewById(R.id.rv2_category_strategy);
        recyclerView4 = (RecyclerView) getView().findViewById(R.id.rv3_category_strategy);
        tv1 = (TextView) getView().findViewById(R.id.tv1_strategy_fragment);
        tv2 = (TextView) getView().findViewById(R.id.tv2_strategy_fragment);
        tv3 = (TextView) getView().findViewById(R.id.tv3_strategy_fragment);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CategoryStrategyActivity.class);
                getActivity().startActivity(intent);
            }
        });

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CategoryLastActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("fly",0);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CategoryLastActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("fly",2);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });


        GridLayoutManager gmanager = new GridLayoutManager(getContext(), 2);

        recyclerView2.setLayoutManager(gmanager);

        GridLayoutManager gmanager1 = new GridLayoutManager(getContext(), 2);

        recyclerView3.setLayoutManager(gmanager1);

        GridLayoutManager gmanager2 = new GridLayoutManager(getContext(), 2);

        recyclerView4.setLayoutManager(gmanager2);

    }

    private void initDateForwebTop() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(getContext(), "没有请求到数据", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        categoryStrategyTop = gson.fromJson(result, CategoryStrategyTop.class);
                        if (categoryStrategyTop.getData().getColumns().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            Toast.makeText(getContext(), "网络解析数据错误", Toast.LENGTH_SHORT).show();
                        }

                    }
                });

            }
        }).start();
    }

    private void initDateForwebAll() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url1)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(getContext(), "网络解析数据错误", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                categoryStrategtNormal = gson.fromJson(result, CategoryStrategyNormal.class);
                if (categoryStrategtNormal.getData().getChannel_groups().size() > 0) {
                    handler.sendEmptyMessage(2);
                } else {
                    Toast.makeText(getContext(), "网络解析数据错误", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    GridLayoutManager gManager = new GridLayoutManager(getContext(), 3);
                    gManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    recyclerView1.setLayoutManager(gManager);
                    cstAdapter = new CategoryStrategyTopAdapter(getContext(), categoryStrategyTop);
                    cstAdapter.setOnItemClickLister(new OnItemClickLister() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(getActivity(), HomeTitleActivity.class);
                            intent.putExtra("id", categoryStrategyTop.getData().getColumns().get(position).getId() + "");
                            getActivity().startActivity(intent);
                        }
                    });
                    recyclerView1.setAdapter(cstAdapter);
                    break;
                case 2:
                    csnAdapter2 = new CategoryStrategyNormalAdapter(1, 0, categoryStrategtNormal, getContext());
                    csnAdapter2.setOnItemClickLister(new OnItemClickLister() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent=new Intent(getActivity(),CategoryRealLastActivity.class);
                            Bundle bundle1=new Bundle();
                            bundle1.putInt("lastId",categoryStrategtNormal.getData().getChannel_groups().get(0).getChannels().get(position).getId());
                            bundle1.putString("lastName",categoryStrategtNormal.getData().getChannel_groups().get(0).getChannels().get(position).getName());
                            intent.putExtras(bundle1);
                            startActivity(intent);
                        }
                    });
                    recyclerView2.setAdapter(csnAdapter2);

                    csnAdapter3 = new CategoryStrategyNormalAdapter(1, 1, categoryStrategtNormal, getContext());
                    csnAdapter3.setOnItemClickLister(new OnItemClickLister() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent=new Intent(getActivity(),CategoryRealLastActivity.class);
                            Bundle bundle1=new Bundle();
                            bundle1.putInt("lastId",categoryStrategtNormal.getData().getChannel_groups().get(1).getChannels().get(position).getId());
                            bundle1.putString("lastName",categoryStrategtNormal.getData().getChannel_groups().get(1).getChannels().get(position).getName());
                            intent.putExtras(bundle1);
                            startActivity(intent);
                        }
                    });
                    recyclerView3.setAdapter(csnAdapter3);

                    csnAdapter4 = new CategoryStrategyNormalAdapter(1, 2, categoryStrategtNormal, getContext());
                    csnAdapter4.setOnItemClickLister(new OnItemClickLister() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent=new Intent(getActivity(),CategoryRealLastActivity.class);
                            Bundle bundle1=new Bundle();
                            bundle1.putInt("lastId",categoryStrategtNormal.getData().getChannel_groups().get(2).getChannels().get(position).getId());
                            bundle1.putString("lastName",categoryStrategtNormal.getData().getChannel_groups().get(2).getChannels().get(position).getName());
                            intent.putExtras(bundle1);
                            startActivity(intent);
                        }
                    });
                    recyclerView4.setAdapter(csnAdapter4);

                    break;
            }
        }
    };

}
